package com.guoning.office_system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoning.office_system.model.Role;
import com.guoning.office_system.model.User;
import com.guoning.office_system.service.RoleService;
import com.guoning.office_system.utils.Constants;
import com.guoning.office_system.utils.JsonResult;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("role")
public class RoleController extends BaseController {
    @Autowired
    RoleService roleService;

    /**
     * 条件查询（分页）
     * @param pageIndex
     * @param pageSize
     * @param enable
     * @param roleCodeOrName
     * @return
     */
    @RequestMapping(value = "/select")
    public JsonResult selectByCondition(@RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(value = "enable", defaultValue = "") Integer enable,
                                        @RequestParam(value = "roleCodeOrName", defaultValue = "") String roleCodeOrName){
        JsonResult jsonResult;
        try {
            PageBean pageBean = new PageBean();
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("enable", enable);
            criteria.addExtField("roleCodeOrName", roleCodeOrName);
            pageBean = roleService.selectRoleByCondition(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS,"查询成功", pageBean);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 新增角色
     * @param request
     * @param role
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult insertSelective(HttpServletRequest request, Role role){
        JsonResult jsonResult;
        try {
            List list = roleService.selectByCode(role);
            if(list.size() != 0){
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "角色编码或角色名称已存在");
                return jsonResult;
            }
            User user = getUserDate(request);
            role.setCreator(user.getUserName());
            int i = roleService.insertSelective(role);
            if(i != 0){
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "成功");
            }else{
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "失败");
            }
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "异常");
        }
        return jsonResult;
    }

    /**
     * 删除角色
     * @param roleList
     * @return
     */
    @RequestMapping(value = "/delete")
    public JsonResult deleteByIds(String roleList){
        JsonResult jsonResult;
        try {
            List<Role> roles = JSONArray.parseArray(roleList, Role.class);
            int i = roleService.deleteByIds(roles);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "删除异常");
        }
        return jsonResult;
    }

    /**
     * 停用/启用角色
     * @param params
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public JsonResult updateEnable(String params){
        JsonResult jsonResult;
        try {
            Map map = JSONObject.parseObject(params, Map.class);
            String enableType = map.get("enableType").toString();
            List<Role> roles = JSONArray.parseArray(map.get("roleList").toString(), Role.class);
            if(enableType.equals("enable")){
                roleService.updateEnableByIds(roles);
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "停用成功");
            }else if (enableType.equals("disable")){
                roleService.updateDisableByIds(roles);
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "启用成功");
            }else{
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "操作失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "删除异常");
        }
        return jsonResult;
    }

    /**
     * 修改角色
     * @param request
     * @param role
     * @return
     */
    @RequestMapping(value = "/update")
    public JsonResult update(HttpServletRequest request, Role role){
        JsonResult jsonResult;
        try {
            List list = roleService.selectByCode(role);
            if(list.size() != 0){
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "角色编码已存在");
                return jsonResult;
            }
            User user = getUserDate(request);
            role.setModifier(user.getUserName());
            int i = roleService.updateByPrimaryKeySelective(role);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "修改成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "修改异常");
        }
        return jsonResult;
    }
}
